package com.blackitheima.test2;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MyTreeMap1 {
    public static void main(String[] args) {
        //创建集合
        TreeMap<Student,String> tm=new TreeMap<>(
                new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        //初条件
                        int result=o1.getAge()-o2.getAge();
                        //次条件
                        result=result==0?o1.getName().compareTo(o2.getName()):result;
                        return result;
                    }
                }
        );
        tm.put(new Student("彭于晏",23),"台湾");
        tm.put(new Student("胡歌",23),"上海");
        tm.put(new Student("胡歌",23),"上海");
        tm.put(new Student("兰陵王",25),"水火");
        tm.put(new Student("妲己",18),"朝歌");

        //提取键值对
        Set<Map.Entry<Student, String>> entries = tm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"--"+value);
        }
    }
}
